package com.sooth.system.role.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.dream.system.config.Page;
import com.sooth.common.core.domain.R;
import com.sooth.common.dream.model.PageModel;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.web.core.BaseController;
import com.sooth.system.role.service.ISysRoleDeptService;
import com.sooth.system.role.service.ISysRoleMenuService;
import com.sooth.system.role.service.ISysRoleService;
import com.sooth.system.role.table.SysRoleDept;
import com.sooth.system.role.table.SysRoleMenu;
import com.sooth.system.role.view.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    private final ISysRoleService roleService;

    private final ISysRoleMenuService roleMenuService;

    private final ISysRoleDeptService roleDeptService;

    /**
     * 获取角色信息列表
     */
    @SaCheckPermission("system:role:query")
    @GetMapping("/list")
    public R<Page<SysRoleVo>> list(SysRoleDto role, PageModel pageModel) {
        return R.ok(roleService.selectPage(role, pageModel));
    }

    /**
     * 不分页获取角色信息
     */
    @SaCheckPermission("system:role:query")
    @GetMapping("/listAll")
    public R<List<SysRoleVo>> listAll(SysRoleDto role) {
        return R.ok(roleService.selectList(role));
    }

    /**
     * 根据角色编号获取详细信息
     *
     * @param id 角色ID
     */
    @SaCheckPermission("system:role:query")
    @GetMapping(value = "/query")
    public R<SysRoleBo> query(@RequestParam Long id) {
        return R.ok(roleService.selectById(id));
    }

    /**
     * 新增角色
     */
    @SaCheckPermission("system:role:add")
    @Log(title = "角色管理", message = "新增角色")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysRoleBo role) {
        return toAjax(roleService.insert(role));

    }

    /**
     * 修改保存角色
     */
    @SaCheckPermission("system:role:edit")
    @Log(title = "角色管理", message = "修改角色")
    @PutMapping("/edit")
    public R<Void> edit(@Validated @RequestBody SysRoleBo role) {
        return toAjax(roleService.updateById(role));
    }


    /**
     * 删除角色
     *
     * @param id 角色ID
     */
    @SaCheckPermission("system:role:remove")
    @Log(title = "角色管理", message = "删除角色")
    @DeleteMapping("/remove")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(roleService.deleteById(id));
    }

    /**
     * 保存角色菜单
     *
     * @param roleMenuVo 角色菜单
     * @return
     */
    @SaCheckPermission("system:role-menu:save")
    @Log(title = "角色菜单管理", message = "保存角色菜单")
    @PostMapping("/menu/save")
    public R<Void> save(@RequestBody SysRoleMenuVo roleMenuVo) {
        roleMenuService.save(roleMenuVo);
        return R.ok();
    }

    /**
     * 查询角色菜单
     *
     * @return
     */
    @SaCheckPermission(value = {"system:role-menu:list", "system:role-menu:save"}, mode = SaMode.OR)
    @Log(title = "角色菜单管理", message = "查询角色菜单")
    @GetMapping("/menu/list")
    public R<List<SysRoleMenu>> list(SysRoleMenuDto roleMenu) {
        List<SysRoleMenu> menuList = roleMenuService.list(roleMenu);
        return R.ok(menuList);
    }

    /**
     * 保存角色数据权限
     *
     * @param roleDept 角色数据权限
     * @return
     */
    @SaCheckPermission("system:role-dept:save")
    @Log(title = "角色数据权限管理", message = "保存角色数据权限")
    @PostMapping("/dept/save")
    public R<Void> save(@RequestBody SysRoleDeptVo roleDept) {
        roleDeptService.save(roleDept);
        return R.ok();
    }

    /**
     * 查询角色数据权限
     *
     * @return
     */
    @SaCheckPermission("system:role-dept:list")
    @Log(title = "角色数据权限管理", message = "查询角色数据权限")
    @GetMapping("/dept/list")
    public R<List<SysRoleDept>> list(SysRoleDeptDto roleDept) {
        List<SysRoleDept> menuList = roleDeptService.list(roleDept);
        return R.ok(menuList);
    }
}
